/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.usb.UsbController
import com.ecarx.systemui.plugin.model.listener.UsbStatusCallBack
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager
import com.ecarx.systemui.plugin.utils.DensityUtils
import com.ecarx.systemui.plugin.utils.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class UsbView : SystemBarButton, UsbStatusCallBack {

    private val TAG = "UsbView"
    private var mUsbController: UsbController? = null
    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {

    }

    private fun registerUsbDiskListener() {
        val usbController = UsbController(context)
        mUsbController = usbController
        mUsbController?.setUsbStatusCallBack(this)
        CoroutineScope(Dispatchers.IO).launch {
            val isMounted = mUsbController?.checkUDiskMounted(context)
            if(isMounted == true){
                withContext(Dispatchers.Main) {
                    onUsbDeviceChanged(true)
                }
            }
        }
    }

    private fun onUsbDeviceChanged(mounted: Boolean) {
        if (mounted) {
            Toast.makeText(
                context,
                resources.getString(R.string.usb_mount_toast),
                Toast.LENGTH_SHORT
            ).show()
            visibility = View.VISIBLE
            showUsbPopupWindow()
        } else {
            Toast.makeText(
                context,
                resources.getString(R.string.usb_unmount_toast),
                Toast.LENGTH_SHORT
            ).show()
            visibility = View.GONE
        }
    }

    private fun unregisterUsbDiskListener() {
        DialogManager.getDialogManager().usbDialog?.dismiss()
        mUsbController?.destroyController(context)
    }

    override fun getContentDescriptionString(): String {
        return "UsbView"
    }

    override fun initView() {
        super.initView()

        setImageDrawable(
            ResourcesCompat
                .getDrawable(
                    resources,
                    R.drawable.ic_status_icon_usb_normal,
                    context.theme
                )
        )
        visibility = View.GONE

        setOnClickListener { v: View? ->
            showUsbPopupWindow()
        }
    }

    private fun showUsbPopupWindow() {
        LogUtils.d(TAG, "showUsbPopupWindow() called ")
        val location = IntArray(2)
        location[0] = (DensityUtils.getScreenWidth() - (resources.getDimension(R.dimen.dp_940) + resources.getDimension(R.dimen.dp_40))).toInt()
        DialogManager.getDialogManager().showUsbDialog(context, location)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()


    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        registerUsbDiskListener()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        unregisterUsbDiskListener()
    }

    override fun onUsbAttached() {
        CoroutineScope(Dispatchers.Main).launch {
            onUsbDeviceChanged(true)
        }
    }

    override fun onUsbDetached() {
        CoroutineScope(Dispatchers.Main).launch {
            onUsbDeviceChanged(false)
        }
    }

}